package com.lanou.zakerproject.city;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.lanou.zakerproject.R;

import java.util.HashMap;
import java.util.List;


/**
 * Created by dllo on 16/1/15.
 */
public class CityAdapter extends BaseExpandableListAdapter {
    private Context context;
    List<String>letter;
    HashMap<String,List<AllCityData.Citys>> allCityData;

    public CityAdapter(Context context, List<String> letter, HashMap<String, List<AllCityData.Citys>> allCityData) {
        this.context = context;
        this.letter = letter;
        this.allCityData = allCityData;
    }


    @Override
    public int getGroupCount() {
        return letter.size();

    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return allCityData.get(letter.get(groupPosition)).size();//get里参数是key
    }

    @Override
    public Object getGroup(int groupPosition) {
        return letter.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return allCityData.get(letter.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_city_group, parent, false);
            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.textViewGroup.setText(letter.get(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_city_child, parent, false);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        childViewHolder.textViewChild.setText(allCityData.get(letter.get(groupPosition)).get(childPosition).getName());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class GroupViewHolder {
        TextView textViewGroup;

        public GroupViewHolder(View view) {
            textViewGroup = (TextView) view.findViewById(R.id.item_city_group);
        }
    }

    class ChildViewHolder {
        TextView textViewChild;

        public ChildViewHolder(View view) {
            textViewChild = (TextView) view.findViewById(R.id.item_city_child);
        }
    }

}
